# encoding: UTF-8
'''
Created: 2014-05-06 10:15

@author: VAN
'''

'''
装饰器就是为已经存在的对象增加额外的功能
功能类似aop。内置的装饰器有三个，分别是staticmethod、classmethod和property，作用分别是把类中定义的实例方法变成静态方法、类方法和类属性。都很少用，基本上是面向对象编程
'''

import functools
def decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print "logger start"
        ret = func(*args, **kwargs)
        print "logger end"
        return ret
    return wrapper

# decorator contain args
def decoratorArgs(decArgs):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            print "logger start"
            ret = func(*args, **kwargs)
            print "logger end"
            return ret
        return wrapper
    return decorator

def timeit(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        import time
        import inspect  # provide self reflect
        start = time.clock()
        ret = func(*args, **kwargs)
        end = time.clock()
        print inspect.getmodule(func).__name__ + "." + func.__name__ + ' timeit aop time : ' , (end - start)
        return ret
    return wrapper


@decorator
@timeit
def objfunc():
    print "original objfunc"
    
@decoratorArgs("decArgs")
def objfunc1(x, y):
    print "original objfunc", x, y




